package com.fh.alipay.controller;

import com.fh.alipay.vo.ApiResult;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import com.fh.alipay.po.Goods;
import com.fh.alipay.service.IGoodsService;
import com.fh.alipay.model.dto.GoodsDTO;
import com.fh.alipay.model.vo.GoodsVO;
import com.fh.alipay.model.query.GoodsQuery;
import com.fh.alipay.model.convert.GoodsConvert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.time.LocalDateTime;
import java.util.Arrays;

import org.springframework.web.bind.annotation.RestController;

/**
 * @author fh
 * @date 2022-05-03
 * @description 控制器
 */
@RestController
@RequestMapping("/aliPay/goods")
public class GoodsController extends BaseController {

    @Resource
    private IGoodsService goodsService;


    /**
     * 获取分页列表
     */
    @PostMapping(value = "/list")
    public ApiResult<IPage<Goods>> list(GoodsQuery query, Page page) {
        IPage<Goods> resultPage = goodsService.getWithPage(page, query);
        return ApiResult.ok(resultPage);
    }

    /**
     * 根据ID获取详情
     */
    @GetMapping("/get")
    public ApiResult<GoodsVO> get(@RequestParam("id") Long id) {
        Goods entity = goodsService.getById(id);
        GoodsVO vo = GoodsConvert.toVO(entity);
        return ApiResult.ok(vo);
    }

    /**
     * 新增
     */
    @PostMapping("/add")
    public ApiResult<String> add(GoodsDTO dto) {
        GoodsQuery query = new GoodsQuery();
        query.setGoodsId(dto.getGoodsId());
        Goods oneByQuery = goodsService.getOneByQuery(query);
        if (null != oneByQuery) {
            return ApiResult.error("该商品已存在");
        }
        Goods po = GoodsConvert.toPO(dto);
        po.setCreateTime(LocalDateTime.now());
        po.setUpdateTime(LocalDateTime.now());
        po.setDelFlag(1);
        boolean save = goodsService.save(po);
        return save ? ApiResult.ok() : ApiResult.fail();
    }

    /**
     * 编辑
     */
    @PostMapping("/update")
    public ApiResult update(@Valid GoodsDTO dto, BindingResult bindingResult) {

        if (bindingResult.hasErrors()) {
            return ApiResult.error("参数有误");
        }
        Goods po = GoodsConvert.toPO(dto);
        po.setUpdateTime(LocalDateTime.now());
        boolean update = goodsService.updateById(po);
        return update ? ApiResult.ok() : ApiResult.fail();
    }

    /**
     * 移除
     */
    @PostMapping("/remove")
    public ApiResult<String> remove(@RequestParam(value = "id") Long id) {
        boolean remove = goodsService.removeById(id);
        return remove ? ApiResult.ok() : ApiResult.fail();
    }

    /**
     * 批量移除
     */
    @PostMapping("/batch/remove")
    public ApiResult batchRemove(@RequestParam(value = "ids") String ids) {
        boolean remove = goodsService.removeByIds(Arrays.asList(ids.split(",")));
        return remove ? ApiResult.ok() : ApiResult.fail();
    }
}
